<template>
  <el-main class="bg table_wrap logistics_delivery">
    <!-- 头部检索 -->
    <div class="div-form">
      <el-form
        ref="elFormRef"
        :model="formInline"
        label-width="90px"
        :style="{ width: '100%' }"
        label-position="left"
      >
      <el-row class="rows row1">
        <el-col :xs="24" :sm="24" :lg="8" class="el_form_search_wrap">
          <el-form-item label="配送状态">
            <el-select v-model="formInline.delivery_status">
              <el-option v-for="o in list_delivery_status" :key="o" :label="o"
                        :value="o">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="8" class="el_form_search_wrap">
          <el-form-item label="签收状态">
            <el-select v-model="formInline.signing_status">
              <el-option v-for="o in list_signing_status" :key="o" :label="o"
                        :value="o">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        </el-row>
      </el-form>
    </div>
    <baseTable
      :tableColumns="columns"
      :requestApi="url_get_list"
      :isShowSelect="true"
       :imgflag="false"
      :vm="vm" 
      v-model:isRefresh="isRefresh"
      :otherParams="formInline"
      @handleSelectionChange="handleSelectionChange"
      @handleCurrentChangeNew="handleCurrentChangeNew"
    >
      <!-- 头部按钮 -->
      <template #baseHeadFunt>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="onReset(true)">重置</el-button>
        <el-button type="danger" v-if="user_group == '管理员' || $check_action('/logistics_delivery/view', 'del')"  class="search_btn_del" @click="delInfo()"
          >删除</el-button
        >
      </template>
      <!-- 操作 -->
      <template #baseTableFunt="{ scope }">
        <el-button
          type="primary"
          size="small"
          @click="router.push('./view?' + field + '=' + scope.row.logistics_delivery_id)"
          v-if="user_group == '管理员' || $check_action('/logistics_delivery/view', 'get') || $check_action('/logistics_delivery/view', 'set')"
          >详情</el-button
        >
        <el-button v-if="scope.row['signing_status'] == '待签收' && (user_group=='管理员' || $check_option('/logistics_delivery/table','sign_for'))"
                    size="small"
                    style="margin: 5px !important;"
                    @click="changeSigning('logistics_delivery_id=' + scope.row['logistics_delivery_id'], scope.row['signing_status'], scope.$index)">
              签收
            </el-button>
      </template>
      <template #baseTableFunt2="{ scope }">
        <el-button
          type="primary"
          size="small"
          @click="router.push('./view?' + field + '=' + scope.logistics_delivery_id)"
          v-if="user_group == '管理员' || $check_action('/logistics_delivery/view', 'get') || $check_action('/logistics_delivery/view', 'set')"
          >详情</el-button
        >
        <el-button v-if="scope['signing_status'] == '待签收' && (user_group=='管理员' || $check_option('/logistics_delivery/table','sign_for'))"
                    size="small"
                    style="margin: 5px !important;"
                    @click="changeSigning('logistics_delivery_id=' + scope['logistics_delivery_id'], scope['signing_status'], scope.$index)">
              签收
            </el-button>
      </template>
      <!-- 特殊表格内容 -->
      <template #tableContent="{ scope, column }">
        <span v-if="column.prop === 'the_date_of_issuance'">
          {{ $toTime(scope.row["the_date_of_issuance"], "YYYY-MM-DD") }}
        </span>
        <span v-if="column.prop === 'ordinary_users'">
         	{{getObj(list_state,scope.row.ordinary_users).nickname}}
        </span>
        <span v-if="column.prop === 'create_time'">{{
          formatDate(scope.row.create_time)
        }}</span>
        <span v-if="column.prop === 'update_time'">{{
          formatDate(scope.row.update_time)
        }}</span>
      </template>
      <template #tableContent2="{ scope, column }">
        <span v-if="column.prop === 'the_date_of_issuance'">
          {{ column.label }}： {{ $toTime(scope["the_date_of_issuance"], "YYYY-MM-DD") }}
        </span>
        <span v-if="column.prop === 'ordinary_users'">
          {{ column.label }}：	{{getObj(list_state,scope.ordinary_users).nickname}}
        </span>
        <span v-if="column.prop === 'create_time'">{{ column.label }}：{{
          formatDate(scope.create_time)
        }}</span>
        <span v-if="column.prop === 'update_time'">{{ column.label }}：{{
          formatDate(scope.update_time)
        }}</span>
      </template>
    </baseTable>
  </el-main>
</template>
<script setup>
import { useRouter } from "vue-router";
let router = useRouter();
import {
  $toTime,
  $fullUrl,
  formatDate,
  $banseUrl,
  $check_action,
} from "@/utils/utils.js";
import { $post, $get } from "@/api/Api.js";
import { Picture } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { ref, toRefs, computed, onMounted } from "vue";
import storageHelper from "@/libs/StorageHelper";
let table = ref("logistics_delivery");
let field = ref("logistics_delivery_id");
let url_get_list = ref("/api/logistics_delivery/get_list");
let url_del = ref("/api/logistics_delivery/del?");
let user_group = storageHelper.getUserInfo().user_group;
 // 配送状态列表
let list_delivery_status = ref(['待取货', '配送中', '已完成']);
// 签收状态列表
let list_signing_status = ref(['待签收', '已签收']);
// 获取用户信息
let list_user = ref([]);
let renderTable=ref(Number(Math.random().toString().substr(3, 12) + Date.now()).toString(36))
let vm=ref({
  id: "logistics_delivery_id",
})
// 列名
const columns = [
  { prop: "order_number", label: "订单号" },
  { prop: "product_name", label: "商品名称" },
  { prop: "purchase_quantity", label: "购买数量" },
  { prop: "total_transaction_amount", label: "交易总额" },
  { prop: "the_date_of_issuance", label: "发货日期" },
  { prop: "delivery_number", label: "配送单号" },
  { prop: "contact_name", label: "联系人" },
  { prop: "user_id", label: "买家" },
  { prop: "shipping_address", label: "收货地址" },
  { prop: "delivery_status", label: "配送状态" },
  { prop: "signing_status", label: "签收状态" },
  { prop: "create_time", label: "创建时间" },
  { prop: "update_time", label: "更新时间" },
];
// 刷新table
let isRefresh = ref(false);
//批量勾选数据
let selectList = ref([]);
//点击当前行数据
let currentRowTableProduct = ref({});
//表格勾选回调
let handleSelectionChange = (val) => {
  selectList.value = val.map((item) => {
    return item.logistics_delivery_id;
  });
};
//点击当前行
let handleCurrentChangeNew = (val) => {
  currentRowTableProduct.value = val;
};
// 批量删除删除
let delInfo = () => {
  ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      selectList.value.map((item) => {
        delFun(item);
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
};

// 调用删除方法
let delFun = (id) => {
  $get(url_del.value + "logistics_delivery_id=" + id).then((json) => {
    if (json.result) {
      ElMessage.success("删除成功");
    } else if (json.error) {
      ElMessage.error(json.error.logistics_delivery);
    }
    isRefresh.value = true;
  });
};

let table_class = ({ row, column, rowIndex, columnIndex }) => {
  return "table_class";
};
// 头部搜索// 搜索
let formInline = ref({
  signing_status: "",
  delivery_status: "",
  login_time: "",
  create_time: "",
  orderby: `create_time desc`
});
// 搜索
let onSubmit = () => {
  isRefresh.value = true;
};
// 重置
let onReset = (refresh) => {
  formInline.value = {
    signing_status: "",
    delivery_status: "",
  };
  isRefresh.value = refresh;
};
//获取用户
let get_list_user = async() => {
  var json =  await $get('/api/user/get_list')
  if (json.result) {
        list_user.value = json.result.list;
      } else if (json.error) {
        console.error(json.error);
      }

};
let changeSigning=(query, status, index)=>{
      var url = "/api/logistics_delivery/set?" + query
      status=status == "待签收" ? '已签收' : "待签收"
      $post(url, {'signing_status': status}).then((json, status) => {
        if (json.result) {
          isRefresh.value = true;
          renderTable.value = Number(Math.random().toString().substr(3, 12) + Date.now()).toString(36)
        } else if (json.error) {
          ElMessage.error(json.error.messag);
          
        } else {
          ElMessage.error('服务器连接失败！');
        }
      });
    }
      // 查询数组对象某个是否包含某个值,返回对应对象
	let getObj=(arr,val) =>{
		let obj = arr.find(item => item.value == val)
		return obj;
	}
onMounted(() => {
  get_list_user()

});
</script>
<style lang="less" scoped>
.div-form {
  padding: 10px;
}
</style>
